package com.tgy.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * https://leetcode-cn.com/problems/spiral-matrix/
 */

public class _54_螺旋矩阵_I {

    public static List<Integer> spiralOrder(int[][] matrix) {

        int left = 0,right = matrix[0].length - 1, top = 0, bottom = matrix.length - 1;

        List<Integer> res = new ArrayList<>();

        while (left <= right && top <= bottom) {

            for (int i = left; i <= right ; i++) {

                res.add(matrix[top][i]);
            }

            top++;

            if (top > bottom) {

                break;
            }

            for (int i = top; i <= bottom ; i++) {

                res.add(matrix[i][right]);
            }
            right--;

            if (left > right) {
                break;
            }

            for (int i = right; i >= left ; i--) {

                res.add(matrix[bottom][i]);
            }

            bottom--;

            for (int i = bottom; i >= top ; i--) {

                res.add(matrix[i][left]);
            }

            left++;
        }

        return res;
    }

    public static void main(String[] args) {

//        int[][] matrix = {{1,2,3},{4,5,6},{7,8,9}};
//        int[][] matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        int[][] matrix = {
                          {1,2,3},
                          {4,5,6},
                          {7,8,9},
                          {10,11,12}
                    };
        List<Integer> res = spiralOrder(matrix);
        System.out.println(res);
    }
}
